package oct2013.linkednode;

public class RemoveDuplicatesfromSortedListII {
	public ListNode deleteDuplicates(ListNode head) {
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode prev = dummy;
		ListNode cur = head;
		while (cur != null) {
			if (cur.next == null || cur.next.val != cur.val) {
				prev = cur;
				cur = cur.next;
			} else {
				while (cur.next != null && cur.next.val == cur.val) {
					cur = cur.next;
				}
				cur = cur.next;
				prev.next = cur;
			}
		}
		return dummy.next;
	}
}
